<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                修改房源
            </el-header>
            <el-main>
                <el-form ref="hrom" :rules="rules" :model="hrom" label-width="115px"
                         label-position="left">
                    <el-form-item label="名称 :" prop="name">
                        <el-input v-model="hrom.name" placeholder="请输入名称"></el-input>
                    </el-form-item>
                    <el-form-item label="房号 :" prop="code">
                        <el-input v-model="hrom.code" placeholder="请输入房号"></el-input>
                    </el-form-item>
                    <el-form-item label="面积(㎡) :" prop="mea">
                        <el-input v-model="hrom.mea" placeholder="请输入面积"></el-input>
                    </el-form-item>
                    <el-form-item label="单价(元) :" prop="upic">
                        <el-input v-model="hrom.upic" placeholder="请输入单价"></el-input>
                    </el-form-item>
                    <el-form-item label="招商状态 : " prop="cbo">
                        <el-select v-model="hrom.cbo" filterable placeholder="请选招商状态" style="width: 100%;">
                            <el-option
                                    v-for="inst in cboOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.val">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="房源用途 : " prop="purp">
                        <el-select v-model="hrom.purp" filterable placeholder="请选择房源用途" style="width: 100%;">
                            <el-option
                                    v-for="inst in purpOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.val">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="装修类型 : " prop="trimType">
                        <el-select v-model="hrom.trimType" filterable placeholder="请选择装修类型" style="width: 100%;">
                            <el-option
                                    v-for="inst in trimTypeOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.val">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="计价单位 : " prop="punt">
                        <el-select v-model="hrom.punt" filterable placeholder="请选择计价单位" style="width: 100%;">
                            <el-option
                                    v-for="inst in puntOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.val">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../utils/webUtils.js';
    export default {
        name: "editHrom",
        props: {
            hromUuid: {
                type: String
            }
        },
        data() {
            return {
                formAlignLeft: 'left',
                hrom: {
                    uuid: this.hromUuid,
                    name: '',
                    code: '',
                    mea: '',
                    upic: '',
                    cbo: '',
                    purp: '',
                    trimType: '',
                    punt: ''
                },
                cboOptions: [],
                purpOptions: [],
                trimTypeOptions: [],
                puntOptions: [],
                rules: {
                    name: [
                        {required: true, message: '房源名称不得为空', trigger: 'blur'},
                    ],
                    code: [
                        {required: true, message: '房号不得为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        //深度监听父组件的props传值
        watch: {
            hromUuid(val) {
                this.hrom.uuid = val;
            }
        },
        mounted() {
            this.queryHrom();
            this.queryCbo();
            this.queryPurp();
            this.queryTrimType();
            this.queryPunt();
        },
        methods: {
            //根据代码获取招商参数
            queryCbo() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/para/code/OPE_CBO",
                        params: {}
                    })
                    .then(response => {
                        this.cboOptions = response.data.subParas;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //根据代码获取房源用途参数
            queryPurp() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/para/code/OPE_HROM_PURPOSE",
                        params: {}
                    })
                    .then(response => {
                        this.purpOptions = response.data.subParas;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //根据代码获取装修类型参数
            queryTrimType() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/para/code/OPE_HROM_TRIM",
                        params: {}
                    })
                    .then(response => {
                        this.trimTypeOptions = response.data.subParas;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //根据代码获取计价单位参数
            queryPunt() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/para/code/OPE_PUNIT",
                        params: {}
                    })
                    .then(response => {
                        this.puntOptions = response.data.subParas;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //通过uuid查询当前对象信息
            queryHrom() {
                this.axios.request({
                    method: "get",
                    url: "./ope/kdi/hrom/" + this.hrom.uuid,
                    headers: {},
                    params: {}
                }).then(response => {
                    this.insertHrom(response.data);
                }).catch(error => {
                    console.log(error);
                });
            },
            insertHrom(hrom) {
                this.hrom.name = hrom.name;
                this.hrom.code = hrom.code;
                this.hrom.mea = hrom.mea;
                this.hrom.upic = hrom.upic;
                this.hrom.cbo = hrom.cbo;
                this.hrom.purp = hrom.purp;
                this.hrom.trimType = hrom.trimType;
                this.hrom.punt = hrom.punt;
            },
            submit() {
                this.$refs['hrom'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.axios.request({
                        method: "put",
                        url: "./ope/kdi/hrom/" + this.hrom.uuid,
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: replaceSpacesObj(this.hrom)
                    }).then(() => {
                        this.info = '修改成功,两秒后关闭';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.$emit("editState", false);
                            this.info = '';
                        }, 2000);
                    }).catch(error => {
                        console.log(error);
                        this.info = "修改失败";
                        this.infoShow = true;
                    });

                });
            },
            cleanHrom() {
                this.hrom = {
                    name: '',
                    code: '',
                    instUuid: '',
                    instName: '',
                    creCode: '',
                    linkMan: '',
                    linkTel: '',
                    userUuid: '',
                    userName: ''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanHrom();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 334px;
        height: 100%;
        z-index: 100;
    }


</style>